﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RTUParameterConfig.Class;

namespace RTUParameterConfig.Controls
{
    public partial class Flowmeters1 : UserControl
    {
        public int intIndex;
        public int DeviceSelectNo;//设备选择序号
        public ParameterConfig mf;

        public Flowmeters1(ParameterConfig mfs)
        {
            InitializeComponent();
            mf = mfs;
            cbbLy.Properties.Items.AddRange(Common.WaterLevelList);
            cbbLy.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化参数
        /// </summary>
        /// <param name="type">设备类型</param>
        /// <param name="index">设备标识</param>
        /// <param name="intDeviceSelectNo">设备选择序号</param>
        public void InitValue(int type, int index, int intDeviceSelectNo)
        {
            gc.Text = Common.FlowmeterList[intDeviceSelectNo].ToString();
            DeviceSelectNo = intDeviceSelectNo;
            intIndex = index;
            this.Tag = type;
            this.TabIndex = (type + 1) * 10 + index;
            cbbType.SelectedIndex = index;
        }

        /// <summary>
        /// 设置流量计配置
        /// </summary>
        public void SetValue()
        {
            cbbType.SelectedIndex = intIndex;
            if (intIndex == 0)
            {
                cbbLy.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterLevelDeviceSelect[0];
                nud2.Value = ParameterConfig.rtuConfigInfo.WaterLevel_Addr[0];
                nud1.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelHight1.Reverse<byte>().ToArray(), 0) / 1000.0);
            }

            if (intIndex == 1)
            {
                cbbLy.SelectedIndex = ParameterConfig.rtuConfigInfo.WaterLevelDeviceSelect[1];
                nud2.Value = ParameterConfig.rtuConfigInfo.WaterLevel_Addr[1];
                nud1.Value = Convert.ToDecimal(BitConverter.ToUInt32(ParameterConfig.rtuConfigInfo.WaterLevelHight2.Reverse<byte>().ToArray(), 0) / 1000.0);
            }
        }

        /// <summary>
        /// 获取流量计配置
        /// </summary>
        public void GetValue()
        {
            intIndex = cbbType.SelectedIndex;
            if (intIndex == 0)
            {
                ParameterConfig.rtuConfigInfo.WaterFlow_Enable |= 0x0001 << 0;
                //ParameterConfig.rtuConfigInfo.WaterLevel_Enable |= 0x0001 << 0;
                //ParameterConfig.rtuConfigInfo.WaterFlow_Addr[0] = Convert.ToByte(cbbLy.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterLevelDeviceSelect[0] = Convert.ToByte(cbbLy.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterLevel_Addr[0] = Convert.ToByte(nud2.Value);
                ParameterConfig.rtuConfigInfo.WaterLevelHight1 = BitConverter.GetBytes(Convert.ToUInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowDeviceSelect[0] = Convert.ToByte(DeviceSelectNo);
            }

            if (intIndex == 1)
            {
                ParameterConfig.rtuConfigInfo.WaterFlow_Enable |= 0x0001 << 1;
                //ParameterConfig.rtuConfigInfo.WaterLevel_Enable |= 0x0001 << 1;
                //ParameterConfig.rtuConfigInfo.WaterFlow_Addr[1] = Convert.ToByte(cbbLy.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterLevelDeviceSelect[1] = Convert.ToByte(cbbLy.SelectedIndex);
                ParameterConfig.rtuConfigInfo.WaterLevel_Addr[1] = Convert.ToByte(nud2.Value);
                ParameterConfig.rtuConfigInfo.WaterLevelHight2 = BitConverter.GetBytes(Convert.ToUInt32(nud1.Value * 1000)).Reverse<byte>().ToArray();
                ParameterConfig.rtuConfigInfo.WaterFlowDeviceSelect[1] = Convert.ToByte(DeviceSelectNo);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("确定要删除设备?", "删除设备", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                Control c = this.Parent;
                c.Controls.Remove(this);
                mf.InitFlowFs();
            }
        }

        private void btnDebug_Click(object sender, EventArgs e)
        {
            MessageInfo.SendMessage(ParameterConfig.mainFormName, ParameterConfig.debugMsg, 3, cbbType.SelectedIndex + 1);
        }

        private void cbbType_SelectedValueChanged(object sender, EventArgs e)
        {
            //mf.InitFlowmetersSource1(cbbType.SelectedIndex, 0, this.TabIndex, Convert.ToInt32(this.Tag));
            mf.InitFlowFs();
        }

        private void btnOther_Click(object sender, EventArgs e)
        {
            try
            {
                WaterLevelSetting wls = new WaterLevelSetting(cbbType.SelectedIndex);
                wls.StartPosition = FormStartPosition.CenterParent;
                wls.ShowDialog();
            }
            catch
            {
                Common.ShowErrorMessage("加载高级设置失败！");
            }
        }

        private void btnImage_Click(object sender, EventArgs e)
        {
            ImageForm image = new ImageForm();
            image.StartPosition = FormStartPosition.CenterParent;
            image.ShowDialog();
        }
    }
}
